﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Customers;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;


namespace MpSoft.Library.BUSS.Customers
{
    #region  Order

    public class OrderBUSS
    {
        private const string ORDER_ALL_KEY = "Mp.order.all-{0}";
        private const string ORDER_BY_ID_KEY = "Mp.order.id-{0}";

        private static void OrderRemoveCache()
        {
            MpCache.RemoveByPattern(ORDER_ALL_KEY);
            MpCache.RemoveByPattern(ORDER_BY_ID_KEY);
        }

        public static int Order_Add(Order obj)
        {
            return Order_Add(obj, null);
        }

        public static int Order_Add(Order obj, List<Cart> ListCart)
        {
            var result = OrderDAO.Order_Add(obj);
            if (result > 0 && ListCart != null)
            {
                OrderProductBUSS.OrderProduct_AddProductToOrder(ListCart, result);
            }
            return result;
        }
        public static int Order_Update(Order obj)
        {
            var result = OrderDAO.Order_Update(obj);
            return result;
        }

        public static int Order_Delete(int OrderID)
        {
            var result = OrderDAO.Order_Delete(OrderID);
            return result;
        }

        public static double GetTotalOrder(List<Cart> listCart)
        {
            double total = 0;
            foreach (var obj in listCart)
                total += obj.Price;
            return total;
        }
        public static int CheckedProductID(List<Cart> listCart, int ProductID)
        {
            for (int i = 0; i < listCart.Count; i++)
                if (listCart[i].ProductID == ProductID)
                    return i;
            return -1;
        }



        public static Order Order_ByID(int OrderID)
        {
            var list = Order_Select(OrderID, -1, false);
            if (list != null)
                return list.FirstOrDefault();
            return null;
        }

        public static List<Order> Order_Select(int OrderID, int CustomerID, bool IsActive)
        {
            return OrderDAO.Order_Select(OrderID, CustomerID, IsActive);
        }

    }

    #endregion
}
